package com.slink.fxfk.common.service.imp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.slink.fxfk.common.domain.bo.BMail;
import com.slink.fxfk.common.service.MailService;
import com.slink.fxfk.framework.util.Env;

@Service
public class MailServiceImpl implements MailService {
	private ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

	@Autowired
	private JavaMailSenderImpl javaMail;

	@Autowired
	private ResourceBundleMessageSource messageSource;

	@Autowired
	private Env env;

	/**
	 * 异步发送邮件
	 * 
	 * @param mail
	 */
	private void sendEmail(final List<BMail> mails) {
		this.cachedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				for (BMail mail : mails) {
					try {
						MimeMessage mime = MailServiceImpl.this.javaMail
								.createMimeMessage();
						MimeMessageHelper helper = new MimeMessageHelper(mime,
								true, "utf-8");
						helper.setFrom(mail.getFrom());// 发件人
						helper.setTo(mail.getTo());// 收件人
						helper.setSubject(mail.getSubject());// 邮件主题
						helper.setText(mail.getText(), true);// true表示设定html格式
						MailServiceImpl.this.javaMail.send(mime);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
	}

	@Override
	public void sendMemberActiveEmail(String email, Locale locale, String[] args) {
		BMail bMail = new BMail();
		bMail.setFrom(this.env.getProp("default_email_account"));
		bMail.setTo(email);
		bMail.setSubject(this.messageSource.getMessage(
				"common.member_active_email_subject", null, locale));
		bMail.setText(this.messageSource.getMessage(
				"common.member_active_email_text", args, locale));
		this.sendEmail(Arrays.asList(bMail));
	}

	@Override
	public void sendMemberResetPasswordEmail(String email, Locale locale,
			String[] args) {
		BMail bMail = new BMail();
		bMail.setFrom(this.env.getProp("default_email_account"));
		bMail.setTo(email);
		bMail.setSubject(this.messageSource.getMessage(
				"common.member_resetpassword_email_subject", null, locale));
		bMail.setText(this.messageSource.getMessage(
				"common.member_resetpassword_email_text", args, locale));
		this.sendEmail(Arrays.asList(bMail));
	}

	@Override
	public void sendEmail(List<String> emails, String subject, String text,
			Locale locale) {
		List<BMail> bMails = new ArrayList<BMail>();
		for (String email : emails) {
			BMail bMail = new BMail();
			bMail.setFrom(this.env.getProp("default_email_account"));
			bMail.setTo(email);
			bMail.setSubject(this.messageSource.getMessage(
					"common.menber_email_inboxmsg_subject",
					new String[] { subject }, locale));
			bMail.setText(text.replaceAll("\n", "<br/>"));
			bMails.add(bMail);
		}

		this.sendEmail(bMails);
	}

	@Override
	public void sendInvitations(List<String> emails, String subject,
			String content) {
		List<BMail> bMails = new ArrayList<BMail>();
		for (String email : emails) {
			BMail bMail = new BMail();
			bMail.setFrom(this.env.getProp("default_email_account"));
			bMail.setTo(email);
			bMail.setSubject(subject);
			bMail.setText(content);
			bMails.add(bMail);
		}

		this.sendEmail(bMails);
	}

	@Override
	public void sendAuthCodeEmail(String email, Locale locale, String[] args) {
		BMail bMail = new BMail();
		bMail.setFrom(this.env.getProp("default_email_account"));
		bMail.setTo(email);
		bMail.setSubject(this.messageSource.getMessage(
				"common.menber_email_authcode_subject", null, locale));
		bMail.setText(this.messageSource.getMessage(
				"common.menber_email_authcode_text", args, locale));
		this.sendEmail(Arrays.asList(bMail));

	}

	public void sendPwdAuthCodeEmail(String email, Locale locale, String[] args) {
		BMail bMail = new BMail();
		bMail.setFrom(this.env.getProp("default_email_account"));
		bMail.setTo(email);
		bMail.setSubject(this.messageSource.getMessage(
				"common.menber_password_authcode_subject", null, locale));
		bMail.setText(this.messageSource.getMessage(
				"common.menber_password_authcode_text", args, locale));
		this.sendEmail(Arrays.asList(bMail));

	}

	@Override
	public void sendUpdateEmail(String email, Locale locale, String[] args) {
		BMail bMail = new BMail();
		bMail.setFrom(this.env.getProp("default_email_account"));
		bMail.setTo(email);
		bMail.setSubject(this.messageSource.getMessage(
				"common.menber_email_updateemail_subject", null, locale));
		bMail.setText(this.messageSource.getMessage(
				"common.menber_email_updateemail_text", args, locale));
		this.sendEmail(Arrays.asList(bMail));
	}
}
